<template>
    <div class="orderLog StockDrugTrans">
        <div style="height: 610px;">
            <Table :columns="tableColumns" :loading="loading" :data-source="tableData" style="margin-top: 20px;" />
        </div>
        <Pagination style="margin-left: auto;margin-top: 20px;" :key="pageSize + 'Pagination' + currentPage" :count="total"
            :page-size="pageSize" :pageNo="currentPage" @onPagechange="handlePageChange"
            @onSizechange="handleSizeChange" />
    </div>
</template>
<script setup lang="ts">
    import {getStockDrugTransList} from '@/api/drug'
    import type { TableProps, TableColumn } from '@/types/table';
    import stores from '@/store';
    import type { GetStockDrugTransResponseList } from '@/types/drug'
    const loading = ref<boolean>(false)
    const tableData = ref<GetStockDrugTransResponseList[]>([])
    const currentPage = ref(1);
    const pageSize = ref(10);
    const total = ref(0);
    const tableColumns: TableColumn[] = [
        { key: 'index', label: '序号', width: 60 },
        { prop: 'transNo', label: '业务单号' },
        { prop: 'transType', label: '业务类型' },
        { prop: 'changeType', label: '变动类型' },
        { prop: 'kcbd', label: '库存变动' },
        { prop: 'transTime', label: '变动时间' },
    
    ]
    
    const handlePageChange = (page: number) => {
        currentPage.value = page;
        getStockDrugTransListApi()
    };
    const handleSizeChange = (size: number) => {
        currentPage.value = 1;
        pageSize.value = size;
        getStockDrugTransListApi()

    };
    const dataType = defineProps<{ storageId:any, drugId:any }>();
    //业务类型
    const drugTransTypeEnum =  ref<any>(stores.useBaseDataStore().getEnumSelect('drugTransTypeEnum'))
    //变动类型
    const drugChangeTypeEnum =  ref<any>(stores.useBaseDataStore().getEnumSelect('drugChangeTypeEnum'))

    const getStockDrugTransListApi = async()=>{
        loading.value = true

        let data = {
            storageId:dataType.storageId,
            drugId:dataType.drugId,
            pageNo:currentPage.value,
            pageSize:pageSize.value,
        }
        let res = await getStockDrugTransList(data)
         if(res.code != '200'){
            //ElMessage.error(res.msg);
            return
        }
        total.value = res.data.totalCount
        res.data.list.forEach(item=>{
            for (let index2 = 0; index2 < drugTransTypeEnum.value.length; index2++) {
                let item2 = drugTransTypeEnum.value[index2];
                if (item && item.transType == item2.value) {
                    item.transType = item2.text
                }
            }

            for (let index2 = 0; index2 < drugChangeTypeEnum.value.length; index2++) {
                let item2 = drugChangeTypeEnum.value[index2];
                if (item && item.changeType == item2.value) {
                    item.changeType = item2.text
                }
            }

            item.kcbd = item.stockQty + item.packUnitText + item.minStockQty + item.minPackUnitText

        })
        tableData.value = res.data.list
        loading.value = false

    }
    getStockDrugTransListApi()
</script>

<style lang="scss">
    .orderLog{
        position: relative;
        height: auto !important;
        .table-pagination{
            width: 100% !important;
        }
    }
    .StockDrugTrans .table-pagination{
        bottom: 10px;
    }
</style>